package study.saas.web.portal.sys.admin;

import com.alibaba.dubbo.config.annotation.Reference;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.ListUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import study.saas.common.util.CsvExportUtil;
import study.saas.common.util.ExcelUtil;
import study.saas.common.viewmodel.ApiResultV2;
import study.saas.order.entity.Commodity;
import study.saas.order.entity.Indent;
import study.saas.order.service.CommodityFacade;
import study.saas.order.service.IndentFacade;
import study.saas.order.service.model.IndentCreateDTO;
import study.saas.order.service.model.IndentModifyDTO;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.*;

/**
 * @author ctc
 */
@Api(tags = "订单管理")
@RestController
@RequestMapping(value = "sys/indent")
@Slf4j
public class IndentController {

    @Reference(version = "1.0.0")
    private IndentFacade indentFacade;
    @Reference(version = "1.0.0")
    private CommodityFacade commodityFacade;


    @ApiOperation(value = "下单")
    @RequestMapping(value = "/placeIndent", method = RequestMethod.POST)
        public ApiResultV2 placeIndent(IndentModifyDTO indent) {
        Commodity commodity = commodityFacade.findCommodity(indent.getName()).getData();
        if (commodity==null){
            return ApiResultV2.error("该商品不存在");
        }else if(commodity.getAmount()<indent.getNumber()){
            return ApiResultV2.error("商品剩余数量不足");
        }else {
            commodityFacade.setAmount(false,indent.getName(),indent.getNumber());
            Indent indent1 = new Indent();
            indent1.setIndentId(UUID.randomUUID().toString().replaceAll("-",""));
            indent1.setCommodityName(indent.getName());
            indent1.setNumber(indent.getNumber());
            indent1.setPrice(indent.getPrice());
            indentFacade.save(indent1);
        }

        return ApiResultV2.ok("下单成功");
    }


    @ApiOperation(value = "查询全部订单")
    @RequestMapping(value = "/getIndentAll", method = RequestMethod.GET)
    public ApiResultV2 getIndentAll() {

        return indentFacade.selectAll();
    }

    @ApiOperation(value = "根据条件查询订单")
    @RequestMapping(value = "/getIndent", method = RequestMethod.GET)
    public ApiResultV2 getIndent(IndentCreateDTO indent) {

        return indentFacade.select(indent);
    }

    @ApiOperation(value = "导出")
    @RequestMapping(value = "/export", method = RequestMethod.GET)
    public void export(HttpServletResponse response) throws IOException {
        try {
            // 查询需要导出的数据
            List<Indent> indentList = indentFacade.selectAll().getData();
            // 构造导出数据结构
            // 设置表头
            String titles = "订单id,商品名称,商品数量,订单价格";
            // 设置每列字段
            String keys = "id,name,number,price";

            // 构造导出数据
            List<Map<String, Object>> datas = new ArrayList<>();
            Map<String, Object> mapInfo;

            for (Indent data : indentList) {
                mapInfo = new HashMap<>(8);
                mapInfo.put("id", data.getIndentId());
                mapInfo.put("name", data.getCommodityName());
                mapInfo.put("number", data.getNumber());
                mapInfo.put("price", data.getPrice());
                datas.add(mapInfo);
            }

            // 设置导出文件前缀
            String fName = "indent_";

            // 文件导出
            OutputStream os = response.getOutputStream();
            CsvExportUtil.responseSetProperties(fName, response);
            CsvExportUtil.doExport(datas, titles, keys, os);
            os.close();
        } catch (Exception e) {
            log.error("导出失败" + e.getMessage(), e);
        }

    }


}
